﻿
var userPageIndex = 1;
var userPageSize = 10;
var isloadding = false;
var mydeskmate = {
    showSuccess: function (con) {
        var data = "<p class='big p20 pl40 pr40'><img src='/app/images/icon_success.jpg' alt='' class='vmiddle mr10'/>" + con + "</p>";
        $.jBox("html:" + data, {
            "title": "提示信息",
            "width": "auto",
            "height": 120,
            "top": "30%",
            "showIcon": false,
            "buttons": {},
            closed: function () { }
        });
        setTimeout(function () {
            $.jBox.close();
        }, 2000);
    },
    showUserInfo: function (cid) {
        $.jBox("iframe:/app/mydeskmate/deskmaterequire.aspx?classid=" + cid, {
            "title": "我的同桌申请",
            "width": 700,
            "height": 550,
            "top": "80px",
            "buttons": {},
            closed: function () {
                document.location.reload();
            }
        });
    },

    parentReload: function () {
        parent.document.location.reload();
    },

    //寻找同桌的用户列表
    loadUser: function (cid, totalCount, user) {
        if (totalCount == 0) {
            return;
        }
        if (isloadding) {
            return;
        }

        if (user != undefined) {
            userPageIndex = 1;
        }
        isloadding = true;
        $("#loadMore").show().find("a").text("加载中...");
        $.get("/app/mydeskmate/load_user.aspx", { classid: cid, pageIndex: userPageIndex, pageSize: userPageSize, user: user, r: Math.random() }, function (data) {
            $("#d_list").append(data);
            addBind($("#d_list"));  //绑定名片窗
            isloadding = false;

            if (data == "" && user != undefined) {
                $("#d_list").html("<div class='txtC big mt20 panel_box'>您搜索的用户名不存在，或者已经有同桌！<div>");
            }

            if (user != undefined) {
                $("#loadMore").hide();
            }

            if (totalCount > userPageIndex * userPageSize) {
                userPageIndex++;
                $("#loadMore").find("a").text("点击查看更多匹配同桌");
            } else {
                $("#loadMore").hide();
            }
        }, "text");
    },

    //我的请求列表
    requestDeskmate: function (uid, cid) {
        var html = "<p class='big pt20 txt_c'>一位同学只能拥有一位同桌，您确定要和Ta成为同桌么？</p>";
        $.jBox("html:" + html, {
            "title": "寻找同桌",
            "width": 430,
            "height": 142,
            opacity: 0.3,
            top: "30%",
            "showIcon": false,
            "buttons": { '确定': 1, '取消': 0 },
            submit: function (v, h, f) {
                if (v == 1) {
                    $.wsajax("/app/mydeskmate/find.aspx", "RequestDeskmate", { userID: uid, classID: cid, r: Math.random() }, function (data) {
                        if (data == "1") {
                            mydeskmate.showSuccess("您已成功发送邀请！请等待对方回应。");
                            $("#request_result_" + uid).html("<a href=\"javascript:\" title=\"一天只能发送一次哦~\" class=\"btnGray mt10\"><span>已发送邀请</span></a>");
                        } else if (data == "4") {
                            $("#request_result_" + uid).html("对方已经有同桌").addClass("red");
                        }
                        else {
                            document.location.reload();
                        }
                    });
                }
                return true;
            },
            closed: function () { }
        });


    },

    loadRequest: function (cid, totalCount) {
        if (totalCount == 0) {
            return;
        }
        if (isloadding) {
            return;
        }

        isloadding = true;
        $("#loadMore").show().find("a").text("加载中...");
        $.get("/app/mydeskmate/load_request.aspx", { classid: cid, pageIndex: userPageIndex, pageSize: userPageSize, r: Math.random() }, function (data) {
            $("#d_list").append(data);
            addBind($("#d_list"));  //绑定名片窗
            isloadding = false;

            if (totalCount > userPageIndex * userPageSize) {
                userPageIndex++;
                $("#loadMore").find("a").text("点击查看更多邀请");
            } else {
                $("#loadMore").hide();
            }
        }, "text");
    },

    //确认加为同桌
    agreeDeskmate: function (uid, cid) {

        var html = "<p class='big pt20 txt_c'>一位同学只能拥有一位同桌，您确定要和Ta成为同桌么？</p>";
        $.jBox("html:" + html, {
            "title": "寻找同桌",
            "width": 430,
            "height": 142,
            opacity: 0.3,
            top: "30%",
            "showIcon": false,
            "buttons": { '确定': 1, '取消': 0 },
            submit: function (v, h, f) {
                if (v == 1) {
                    $.wsajax("/app/mydeskmate/request_list.aspx", "AgreeDeskmate", { userID: uid, classID: cid, r: Math.random() }, function (data) {
                        if (data == "1") {
                            $("#request_result_" + uid).html("<a class='btnGreen' href='/" + cid + "/mydeskmate/'><span>看看新同桌</span></a>");

                            $("#d_list a[onclick^='mydeskmate.AgreeDeskmate']").hide();

                            var html = "<div style='overflow:hidden;padding-top: 10px;'><img src='/app/mydeskmate/images/d_success.jpg' class='fl' data-pinit='registered'><div class='big mt20'>恭喜您们成为同桌！以后要互相鼓励，一起进步哦~</div></div>";
                            $.jBox("html:" + html, {
                                "title": "寻找同桌",
                                "width": 400,
                                "height": 200,
                                opacity: 0.3,
                                top: "30%",
                                "showIcon": false,
                                "buttons": { '看看新同桌': 1, '知道了': 0 },
                                submit: function (v, h, f) {
                                    if (v == 1) {
                                        document.location.href = "/" + cid + "/mydeskmate/";
                                    } else {
                                        document.location.reload();
                                    }
                                    return true;
                                }
                            });

                        } else if (data == "4") {
                            $("#request_result_" + uid).html('<a class="btnGray"><span>已有同桌</span></a>');
                        }
                        else {
                            document.location.reload();
                        }
                    });
                }
                return true;
            },
            closed: function () { }
        });
        return;
    },
    //忽略同桌
    ignoreDeskmate: function (uid, cid) {

        var html = "<p class='big pt20 txt_c'>选择忽略后，这条邀请将被删除，但对方不会被通知。<br/>您确定要忽略对方的同桌邀请吗？</p>";
        $.jBox("html:" + html, {
            "title": "忽略同桌请求",
            "width": 430,
            "height": 150,
            opacity: 0.3,
            top: "30%",
            "showIcon": false,
            "buttons": { '确定': 1, '再想想': 0 },
            submit: function (v, h, f) {
                if (v == 1) {
                    $.wsajax("/app/mydeskmate/request_list.aspx", "IgnoreDeskmate", { userID: uid, classID: cid }, function (data) {
                        if (data == "1") {
                            $("#li_" + uid).slideUp(function () {
                                mydeskmate.showSuccess("已忽略！");
                            });
                        }
                    });
                }
                return true;
            },
            closed: function () { }
        });
        return;
    },
    closeJbox: function () {
        parent.document.location.reload();
    },
    checkSearch: function () {
        if ($("#txtKeyword").val() != "" && $("#txtKeyword").val() != $("#txtKeyword").attr("rel")) {
            return true;
        }
        else {
            $("#txtKeyword").val("");
            $("#txtKeyword").removeClass("gray");
        }
        return false;
    },
    closeMsgDialog: function (t) {
        $("#msgBox_" + t).fadeOut(200);
    },

    addMessage: function (cid, msg, type, successMsg) {
        if (isloadding) {
            return;
        }

        isloadding = true;
        if (isloadding) {
            $.wsajax("/app/mydeskmate/message_deal.aspx", "AddDeskmateMessage", { cid: cid, msg: msg, type: type }, function (data) {
                if (data== "-2") {
                    alert("您提交的内容含有敏感词汇，请检查重新提交！");
                }
                else {
                    $(".msgBox").fadeOut(200);
                    parent.closeJbox(successMsg);
                }
                isloadding = false;

            });
        }
    },

    showAddScrip: function (cid) {
        $(".msgBox").hide();
        var opt = {
            title: "给同桌传纸条",
            width: 450,
            height: 280,
            iframeScrolling: "no",
            closed: function () {
            },
            buttons: null
        };
        $.jBox("iframe:/app/mydeskmate/message_deal.aspx?classid=" + cid, opt);
    },

    showReplyMessage: function (messageID) {

        if ($("#reply_list_" + messageID).is(":visible") || $("#r" + messageID).is(":visible")) {
            $("#r" + messageID).slideUp();
            $("#reply_list_" + messageID).slideUp();
        }
        else {
            if ($.trim($("#reply_list_" + messageID).text()).length > 0) {
                $("#r" + messageID).slideDown();
                $("#reply_list_" + messageID).slideDown();
            }
            else {
                //加载数据
                $.get("/app/mydeskmate/load_replymessage.aspx", { id: messageID, r: Math.random() }, function (data) {

                    if ($("#r" + messageID).length > 0) {

                        $("#r" + messageID).html($("#doreply").html()).find("textarea").keyup(function (e) {
                            if (e.ctrlKey && e.keyCode == 13) {
                                $("#r" + messageID).find("a.btnGreen").click();
                            }
                            mydeskmate.checkSize($(this).val(), $("#r" + messageID).find(".txt_num"), 300);
                        }).end().slideDown().find("a.btnGreen").click(function () {
                            var content = $("#r" + messageID).find("textarea");
                            var strLength = parseInt($.trim(content.val().replace(/\n/g, "").replace(/[^\x00-\xff]/g, "rr")).length / 2);
                            if (content.val() == "") {
                                mydeskmate.lightBorder($("#r" + messageID).find(".reply_textarea"));
                                content.focus();
                                return;
                            } else if (mydeskmate.valSize(content.val()) > 300) {
                                return;
                            }
                            if (isloadding) {
                                return;
                            }

                            isloadding = true;
                            var msg = content.val();
                            if (isloadding) {
                                $.wsajax("/app/mydeskmate/message_deal.aspx", "AddReplyMessage", { messageID: messageID, msg: msg }, function (data) {
                                    if (data== "-2") {
                                        alert("您提交的内容含有敏感词汇，请检查重新提交！");
                                    }
                                    else {
                                        var tmpt = $("#reply_tmp").clone();
                                        $(tmpt).find(".message_txt").text(msg);
                                        if ($.trim($("#reply_list_" + messageID).text()).length == 0) {
                                            $("#reply_list_" + messageID).show();
                                        }
                                        tmpt.hide().prependTo($("#reply_list_" + messageID)).slideDown();
                                        content.val("").focus();
                                    }
                                    isloadding = false;
                                });
                            }

                        }).end().find("a.btnWhite").click(function () {
                            mydeskmate.showReplyMessage(messageID);
                        });
                    }
                    $("#r" + messageID).slideDown(function () {
                        $("#r" + messageID).find(".reply_textarea").focus();
                    });
                    if ($.trim(data) != "") {
                        $("#reply_list_" + messageID).html(data).slideDown();
                    }
                    addBind($("#reply_list_" + messageID));  //绑定名片窗
                });
            }
        }
    },
    labelHide: function (obj, label) {
        if (obj.text() == label) {
            obj.text("");
        }
    },
    labelShow: function (obj, label) {
        if (obj.val() == "") {
            obj.text(label);
        }
    },

    showCancel: function (cid) {
        $.jBox("iframe:/app/mydeskmate/show_cancel.aspx?classid=" + cid, {
            "title": "解除同桌关系",
            "width": 425,
            "height": 200,
            "showIcon": false,
            "iframeScrolling": "no",
            "buttons": null
        });

        $("#jbox-content").find("#txtCancel").keypress(function (e) {
            if (e.ctrlKey && e.keyCode == 13) {
                $("#jbox-content").find("#btnCancel").click();
            }
        });

    },

    cancelDeskmate: function (cid) {

        var content = $("#txtCancel");
        if (content.val() == "") {
            content.focus();
            return;
        }

        $.wsajax("/app/mydeskmate/mydeskmate.aspx", "CancelDeskmate", { cid: cid, msg: content.val() }, function (data) {
            if (data == "-2") {
                alert("您提交的内容含有敏感词汇，请检查重新提交！");
            }
            else {
                parent.closeCancel();
            }
        });
    },

    repealCancelDeskmate: function (cid) {
        var html = "<p class='big p20 pt40 txt_c'>您确定要撤销同桌解除申请么？</p>";
        $.jBox("html:" + html, {
            "title": "撤销同桌解除",
            "width": 430,
            "height": 172,
            "top": "10px",
            opacity: 0.3,
            "showIcon": false,
            "buttons": { '确定': 1, '取消': 0 },
            submit: function (v, h, f) {
                if (v == 1) {
                    $.wsajax("/app/mydeskmate/cancel.aspx", "RepealCancelDeskmate", { classID: cid }, function (data) {
                        document.location.reload();
                    });
                    mydeskmate.showSuccess("您已成功撤销同桌解除！");
                }
            }
        });
    },

    disAgreeCancelDeskmate: function (cid, day) {
        var html = "<p class='p20 pt20 big'>您将有 <span class='green'>" + day + "</span> 天时间来让您的同桌回心转意哦~在这期间，请努力学习并勾搭同桌。<span class='green'>" + day + "</span> 天后若同桌依然坚持解除，你们将不再是同桌咯。</p>";
        $.jBox("html:" + html, {
            "title": "同桌解除",
            "width": 430,
            "height": 172,
            opacity: 0.3,
            "showIcon": false,
            "buttons": { '确定': 1, '取消': 0 },
            submit: function (v, h, f) {
                if (v == 1) {
                    $.wsajax("/app/mydeskmate/cancel.aspx", "DisAgreeCancelDeskmate", { classID: cid }, function (data) {
                        document.location.reload();
                    });
                }
            }
        });
    },

    agreeCancelDeskmate: function (cid) {
        var html = "<p class='p20 pt20 big'>你确定要解除同桌关系么？</p>";
        $.jBox("html:" + html, {
            "title": "同桌解除",
            "width": 280,
            "height": 130,
            opacity: 0.3,
            "showIcon": false,
            "buttons": { '确定': 1, '取消': 0 },
            submit: function (v, h, f) {
                if (v == 1) {
                    $.wsajax("/app/mydeskmate/cancel.aspx", "AgreeCancelDeskmate", { classID: cid }, function (data) {
                        mydeskmate.showSuccess("同桌解除成功！");
                        setTimeout(function () {
                            document.location.reload();
                        }, 3000);
                    });
                }
            }
        });
    },
    checkSize: function (str, num, i) {
        var strLength = parseInt($.trim(str.replace(/\n/g, "").replace(/[^\x00-\xff]/g, "rr")).length / 2);
        //alert(strLength);
        var MaxNum = i;
        if (strLength > MaxNum) {
            num.text(MaxNum - strLength);
            num.css("color", "red");
        } else {
            num.text(MaxNum - strLength);
            num.css("color", "#399003");
        }
    },
    valSize: function (str) {
        var strLength = parseInt($.trim(str.replace(/\n/g, "").replace(/[^\x00-\xff]/g, "rr")).length / 2);
        return strLength;
    },
    lightBorder: function (obj) {
        var showCount = 0; var MyInterval = setInterval(function () {
            if (showCount < 4) {
                if (showCount % 2 == 0)
                { obj.addClass("lightborder"); }
                else
                { obj.removeClass("lightborder"); }
                showCount++;
            }
            else { clearInterval(MyInterval); }
        }, 150); return false;
    }
}
